package com.wfcm.service.impl;

import com.wfcm.dao.DistributionDao;
import com.wfcm.entity.Distribution;
import com.wfcm.service.DistributionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Auther: liu
 * @Date: 2018/7/17 13:43
 * @Description: 分销功能
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DistributionServiceImpl implements DistributionService {

    @Autowired
    private DistributionDao distributionDao;

    @Override
    public Distribution configuration() {
        Distribution distribution = new Distribution();
        distributionDao.configuration().stream().forEach(item -> {
            switch (item.get("key").toString()) {
                case "commission_rate":
                    distribution.setCommissionRate(Integer.valueOf(item.get("value").toString()));
                    break;
                case "high_commission_rate":
                    distribution.setHighCommissionRate(Integer.valueOf(item.get("value").toString()));
                    break;
                case "invite_commission_rate":
                    distribution.setInviteCommissionRate(Integer.valueOf(item.get("value").toString()));
                    break;
                case "condition":
                    distribution.setCondition(Integer.valueOf(item.get("value").toString()));
                    break;
                case "num":
                    distribution.setNum(Integer.valueOf(item.get("value").toString()));
                    break;
            }
        });
        return distribution;
    }

    @Override
    public Boolean searchGoods(Integer goodsId, Integer goodsSort) {
        Long time = System.currentTimeMillis() / 1000;
        return null != distributionDao.searchGoods(goodsId, goodsSort, time.intValue()) ? true : false;
    }

    @Override
    public Integer addRecord(Distribution distribution) {
        return distributionDao.addRecord(distribution);
    }

    @Override
    public void personalCenter(Distribution distribution) {
        Long time = System.currentTimeMillis() / 1000;
        distribution.setTime(time.intValue());
        List<Distribution> recordList = distributionDao.recordList(new Distribution(distribution.getInviterId()));
        distribution.setUserCount(distributionDao.customerCount(distribution));
        distribution.setOrderCount(distributionDao.recordCount(distribution));
        List<Distribution> goodsList = distributionDao.goodsList(distribution);
        for (int i = 0; i < goodsList.size(); i++) {
            goodsList.get(i).setReward(new BigDecimal(goodsList.get(i).getGoodsPrice()
                    * goodsList.get(i).getCommissionRate() / 100)
                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        }
        distribution.setResults(goodsList);
        distribution.setTotalCount(distributionDao.goodsCount(distribution));
        BigDecimal commission = new BigDecimal(0);
        for (Distribution d : recordList) {
            commission = commission.add(new BigDecimal(d.getCommission().toString()));
        }
        distribution.setAmount(commission.doubleValue());
    }

    @Override
    public void orderList(Distribution distribution) {
        distribution.setResults(distributionDao.recordList(distribution));
        distribution.setTotalCount(distributionDao.recordCount(distribution));
    }

    @Override
    public void userList(Distribution distribution) {
        distribution.setType1(distributionDao.customerCount(new Distribution(distribution.getInviterId(), 1)));
        distribution.setType2(distributionDao.customerCount(new Distribution(distribution.getInviterId(), 2)));
        distribution.setType3(distributionDao.customerCount(new Distribution(distribution.getInviterId(), 3)));
        distribution.setType4(distributionDao.customerCount(new Distribution(distribution.getInviterId(), 4)));
        distribution.setResults(distributionDao.customerList(distribution));
        distribution.setTotalCount(distributionDao.customerCount(distribution));
    }

    @Override
    public Distribution queryDistributionByKey(String key) {
        return distributionDao.queryDistributionByKey(key);
    }

    @Override
    public Distribution searchGoodsInfo(Integer goodsId, Integer goodsSort) {
        return distributionDao.searchGoodsInfo(goodsId, goodsSort);
    }
}
